{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "### supermind数据 机器学习训练v3()\n",
    "    特征：+行业涨停率 \n",
    "    首次涨停模型：v5_final"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import time,datetime\n",
    "from datetime import date\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import log_loss\n",
    "import lightgbm as lgb\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score\n",
    "execfile('yanmu_v3/yanmu_util.py')\n",
    "execfile('yanmu_v3/yanmu_label.py')\n",
    "execfile('yanmu_v3/train_util.py')\n",
    "\n",
    "# execfile('super1.py')\n",
    "# execfile('super1_util.py')\n",
    "# 获取当前日期\n",
    "today_str = date.today().strftime(\"%m_%d\")\n",
    "out_path = 'output/'\n",
    "print(today_str)\n",
    "lgb.__version__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_df = pd.read_csv('output/v3_train_feature.csv')\n",
    "train_df = train_df[(train_df['date'] > '2017-01-01')]\n",
    "p_col(train_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# list(train_df.columns)\n",
    "# train_df[['date','code','turnover']]\n",
    "# p_col(train_df)\n",
    "train_df = train_df.dropna()\n",
    "print(len(train_df))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# single_df = train_df[train_df['code']=='000001.SZ'][['date', 'code', 'float_mv','open_gap_ratio']]\n",
    "# single_df\n",
    "# print(list(train_df.columns))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# train_df.sort_values(by=['date'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6",
   "metadata": {},
   "source": [
    "### 一、10cm个股首次涨停训练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [],
   "source": [
    "execfile('yanmu_v3/yanmu_label.py')\n",
    "execfile('yanmu_v3/train_10.py')\n",
    "f_zt_1_df = filter_10cm_zt_1(train_df)\n",
    "analyze_label_distribution(f_zt_1_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# f_zt_1_df[['date', 'code', 'float_mv','is_high_open_gap']]\n",
    "# f_zt_1_df.to_csv('output/v3_train_zt1_feature.csv',index=False,encoding='utf-8')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# f_zt_1_df[f_zt_1_df['ret3']>0.12][['date','code','ret3']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10",
   "metadata": {},
   "outputs": [],
   "source": [
    "execfile('yanmu_v3/train_10.py')\n",
    "train_10_recall_zt_1(f_zt_1_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "execfile('yanmu_v3/train_10.py')\n",
    "train_10_precision_zt_1(f_zt_1_df)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12",
   "metadata": {},
   "outputs": [],
   "source": [
    "# list(f_zt_1_df.columns)\n",
    "# execfile('yanmu_v3/analysis.py')\n",
    "# get_importance('input/v3_10_high_recall_zt_1.txt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13",
   "metadata": {},
   "outputs": [],
   "source": [
    "# f_df = f_zt_1_df[['date','code','ret1','ret2','ret3','label']]\n",
    "# f_df = f_df[f_df['label']==3]\n",
    "# f_df.sort_values(by=['date'])[-50:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14",
   "metadata": {},
   "outputs": [],
   "source": [
    "# # list(f_zt_pre_df.columns)\n",
    "# execfile('yanmu_v3/analysis.py')\n",
    "\n",
    "\n",
    "#     # 运行互信息分析\n",
    "# mi_results = mutual_info_analysis(f_zt_1_df, target_col='label',n_bins=15,sample_size=1000000,  top_n=140)\n",
    "    \n",
    "# mi_results.to_csv('input/feature_mi_importance_zt1.csv', index=False)\n",
    "    \n",
    "#     # 打印最重要的20个特征\n",
    "# print(\"整体最重要的20个特征:\")\n",
    "# print(mi_results.head(20))\n",
    "    \n",
    "# if 'mi_score_3' in mi_results.columns:\n",
    "#     print(\"\\n类别3最重要的20个特征:\")\n",
    "#     print(mi_results.sort_values('mi_score_3', ascending=False).head(20))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15",
   "metadata": {},
   "outputs": [],
   "source": [
    "# mi_results[120:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "17",
   "metadata": {},
   "source": [
    "#### 二、首次出现涨幅大于4%"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18",
   "metadata": {},
   "outputs": [],
   "source": [
    "execfile('yanmu_v3/yanmu_label.py')\n",
    "execfile('yanmu_v3/train_10.py')\n",
    "f_gt4_df = filter_10cm_gt4(train_df)\n",
    "analyze_label_distribution(f_gt4_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "19",
   "metadata": {},
   "outputs": [],
   "source": [
    "# f_zt_pre_df.sort_values(by=['date'])[['date',\n",
    "#  'code','h_w20']]\n",
    "# list(f_zt_pre_df.columns)\n",
    "# f_gt4_df.to_csv('output/v3_train_gt4_feature.csv',index=False,encoding='utf-8')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20",
   "metadata": {},
   "outputs": [],
   "source": [
    "execfile('yanmu_v3/train_10.py')\n",
    "train_recall_zt_after(f_gt4_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "21",
   "metadata": {},
   "outputs": [],
   "source": [
    "   \n",
    "execfile('yanmu_v3/train_10.py')\n",
    "train_precision_zt_after(f_gt4_df)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22",
   "metadata": {},
   "source": [
    "# "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23",
   "metadata": {},
   "outputs": [],
   "source": [
    "f_df = f_gt4_df[['date','code','ret1','ret2','ret3','label']]\n",
    "f_df = f_df[f_df['label']==3]\n",
    "f_df = f_df[f_df['date'] > '2025-01-01']\n",
    "\n",
    "f_df.sort_values(by=['date'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "24",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
